﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace KeyLock
{
    public partial class Options : Form
    {
        #region Attributs
        Component1 _comp;

        bool _extend;
        const int _collapse = 160;
        const int _expand = 275;
        #endregion

        #region Constructeur
        public Options(Component1 comp)
        {
            InitializeComponent();
            this._comp = comp;
            this._extend = false;
            Properties.Settings.Default.Reload();
            this.Num_NbToucheMaj.Value = Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.Checked = Properties.Settings.Default.Alert;
            this.Ckb_Ballon.Checked = Properties.Settings.Default.Baloon;
            this.Pct_Lock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconLock));
            this.Pct_Unlock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock));

            this.Size = new Size(this.Size.Width, _collapse);
            this._extend = false;
            Btn_Options.Text = "Options [+]";
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        new public void Show(bool baloon)
        {
            Properties.Settings.Default.Reload();
            this.Num_NbToucheMaj.Value = Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.Checked = Properties.Settings.Default.Alert;
            this.Ckb_Ballon.Checked = Properties.Settings.Default.Baloon;
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(baloon);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(baloon);
            this.Pct_Lock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconLock));
            this.Pct_Unlock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock));

            base.Show();
        }

        new public void Hide()
        {
            Expand(false);

            base.Hide();

        }

        public bool Expand(bool e)
        {
            //160 275
            if (e)
            {
                this.Size = new Size(this.Size.Width, _expand);
                this._extend = true;
                Btn_Options.Text = "Options [-]";
            }
            else
            {
                this.Size = new Size(this.Size.Width, _collapse);
                this._extend = false;
                Btn_Options.Text = "Options [+]";
            }
            return this._extend;
        }

        public bool Expand()
        {
            //160 275
            if (this._extend)
            {
                this.Size = new Size(this.Size.Width, _collapse);
                this._extend = false;
                Btn_Options.Text = "Options [+]";
            }
            else
            {
                this.Size = new Size(this.Size.Width, _expand);
                this._extend = true;
                Btn_Options.Text = "Options [-]";
            }
            return this._extend;
        }
        #endregion

        #region Controles
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.NbTouche = this.Num_NbToucheMaj.Value;
            Properties.Settings.Default.Alert = this.Ckb_Alert.Checked;
            Properties.Settings.Default.Baloon = this.Ckb_Ballon.Checked;
            Properties.Settings.Default.IconLock = Texte.CodeSetting(Properties.Settings.Default.IconLock);
            Properties.Settings.Default.IconUnlock = Texte.CodeSetting(Properties.Settings.Default.IconUnlock);
            Properties.Settings.Default.Save();
            this.Hide();
        }

        private void Num_NbToucheMaj_ValueChanged(object sender, EventArgs e)
        {
            Gpb_Alert.Text = "Si plus de " + Num_NbToucheMaj.Value + " touches en Majuscules ont été appuyées :";
        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.Num_NbToucheMaj.Value = Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.Checked = Properties.Settings.Default.Alert;
            this.Ckb_Ballon.Checked = Properties.Settings.Default.Baloon;
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
            this.Pct_Lock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconLock));
            this.Pct_Unlock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock));
            this.Hide();
        }

        private void Btn_Default_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.IconLock = Properties.Settings.Default.OriginalLock;
            Properties.Settings.Default.IconUnlock = Properties.Settings.Default.OriginalUnlock;
            this.Pct_Lock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconLock));
            this.Pct_Unlock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock));
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
        }

        private void Btn_Options_Click(object sender, EventArgs e)
        {
            Expand();
        }

        private void Pct_Lock_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Fichier Icones (*.ico)|*.ico";
            openFileDialog1.Title = "Icone Lock";
            openFileDialog1.InitialDirectory = Texte.DecodeSetting("[InstallDir]\\Resources");
            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;
            if (file.EndsWith(".ico") && System.IO.File.Exists(file))
            {
                Properties.Settings.Default.IconLock = Texte.CodeSetting(file);
                this.Pct_Lock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconLock));
                this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
                this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
            }
        }

        private void Pct_Unlock_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Fichier Icones (*.ico)|*.ico";
            openFileDialog1.Title = "Icone Lock";
            openFileDialog1.InitialDirectory = Texte.DecodeSetting("[InstallDir]\\Resources");
            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;
            if (file.EndsWith(".ico") && System.IO.File.Exists(file))
            {
                Properties.Settings.Default.IconUnlock = Texte.CodeSetting(file);
                this.Pct_Unlock.Image = Image.FromFile(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock));
                this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
                this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
            }
        }
        #endregion
    }
}
